# Set camke minumun version
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(CMAKE_BUILD_TYPE "Debug")

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

message("cmake-system-${CMAKE_SYSTEM_NAME}")
message("cmake-system-${CMAKE_HOST_SYSTEM_NAME}")
IF (NOT DEFINED QUARK_SYSTEM)
    IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
        SET(QUARK_SYSTEM 1)
    ELSEIF(CMAKE_HOST_SYSTEM_NAME  MATCHES "Linux")
        SET(QUARK_SYSTEM 1)    
    ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Freertos")
        SET(QUARK_SYSTEM 2)
    ENDIF()
ENDIF()

# Set toolchain
SET(TOOL_CHAIN_DIR $ENV{TOOL_CHAIN_PATH})
IF(TOOL_CHAIN_DIR)
    SET(CMAKE_SYSTEM_NAME Linux)
    SET(CMAKE_FIND_ROOT_PATH "$ENV{TOOL_CHAIN_ROOT}")
    SET(CMAKE_C_COMPILER "$ENV{TOOL_CHAIN_PATH}/$ENV{CROSS_COMPILE}-gcc")
    SET(CMAKE_CXX_COMPILER "$ENV{TOOL_CHAIN_PATH}/$ENV{CROSS_COMPILE}-g++")

    # search for programs in the build host directories (not necessary)
    SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    # for libraries and headers in the target directories
    SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
ENDIF(TOOL_CHAIN_DIR)

SET(PLATFORM_LIBRARY_PATH $ENV{PLATFORM_LIBRARY_PATH})
IF(PLATFORM_LIBRARY_PATH)
    message("PLATFORM_LIBRARY_PATH is $ENV{PLATFORM_LIBRARY_PATH}")
    LINK_LIBRARIES("$ENV{PLATFORM_LIBRARY_PATH}")
ENDIF()

SET(PLATFORM_INC_PATH $ENV{PLATFORM_INC_PATH})
IF(PLATFORM_INC_PATH)
    message("PLATFORM_INC_PATH is $ENV{PLATFORM_INC_PATH}")
    INCLUDE_DIRECTORIES("$ENV{PLATFORM_INC_PATH}")
ENDIF()

# Root project
PROJECT(ROOT_PROJECT)

SET(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib")
SET(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
SET(INSTALL_DIR "${PROJECT_SOURCE_DIR}/out")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})

ADD_DEFINITIONS(-DCONFIG)
ADD_DEFINITIONS(-DQUARK_VERSION="1.0")
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/config)

INCLUDE_DIRECTORIES(${all_incs})
INCLUDE_DIRECTORIES(${IDF_PATH}/components/soc/${TARGET}/include)

# Install one module
FUNCTION(ADD_MODULE_INSTALL arg)
    ADD_CUSTOM_TARGET(${ARGV0})
    ADD_CUSTOM_COMMAND(
        TARGET ${ARGV0}
        POST_BUILD
        COMMAND make install
    )
    LIST(LENGTH ARGV argv_len)
    SET(i 1)
    WHILE(i LESS ${argv_len})
         LIST(GET ARGV ${i} argv_value)
         ADD_DEPENDENCIES(${ARGV0} ${argv_value})
         MATH(EXPR i "${i} + 1")
    ENDWHILE()
ENDFUNCTION()

FUNCTION(ADD_OUTPUT_LIBRARY arg)
    IF(NOT ((${ARGV0} MATCHES "_shared") AND (${SUPPORT_SO} MATCHES n)))
        ADD_LIBRARY(${ARGV})
    ENDIF()
ENDFUNCTION()

FUNCTION(ADD_MODULE_DEPENDENCIES arg)
    IF(NOT ((${ARGV0} MATCHES "_shared") AND (${SUPPORT_SO} MATCHES n)) )
        ADD_DEPENDENCIES(${ARGV})
    ENDIF()
ENDFUNCTION()

FUNCTION(ADD_TARGET_LINK_LIBRARIES arg)
    IF(NOT ((${ARGV0} MATCHES "_shared") AND (${SUPPORT_SO} MATCHES n)) )
        TARGET_LINK_LIBRARIES(${ARGV})
    ENDIF()
ENDFUNCTION()

FUNCTION(SET_MODULE_PROPERTIES arg)
    IF(NOT ((${ARGV0} MATCHES "_shared") AND (${SUPPORT_SO} MATCHES n)) )
        SET_TARGET_PROPERTIES(${ARGV})
    ENDIF()
ENDFUNCTION()

FUNCTION(INSTALL_OUTPUT arg)
    IF(NOT ((${ARGV1} MATCHES "_shared") AND (${SUPPORT_SO} MATCHES n)) )
        INSTALL(${ARGV})
    ENDIF()
ENDFUNCTION()

# mqtt
ADD_DEFINITIONS(-DMQTT_TASK=1)
ADD_DEFINITIONS(-DTENCENT_IOT=1)
ADD_DEFINITIONS(-DQUARK_SYSTEM=${QUARK_SYSTEM})
# Add all sub directory,it is one module
ADD_SUBDIRECTORY(framework/json)
ADD_SUBDIRECTORY(framework/md5)
ADD_SUBDIRECTORY(framework/base64)
ADD_SUBDIRECTORY(framework/logs)
ADD_SUBDIRECTORY(framework/common)
ADD_SUBDIRECTORY(framework/system)

ADD_SUBDIRECTORY(driver/wifi)
ADD_SUBDIRECTORY(driver/network)
ADD_SUBDIRECTORY(driver/http)
ADD_SUBDIRECTORY(driver/device)
ADD_SUBDIRECTORY(driver/ans)
ADD_SUBDIRECTORY(driver/mqtt)
ADD_SUBDIRECTORY(driver/player)

ADD_SUBDIRECTORY(sdk)

ADD_SUBDIRECTORY(demo/examples)

IF(QUARK_SYSTEM EQUAL 1)
    ADD_SUBDIRECTORY(demo/linux_test)
ENDIF()